home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue62 / Alfresco / AAHpMin.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-08-21  |  1.6 KB  |  71 lines

  1. {*********************************************************}
  2. {* AAHpMin                                               *}
  3. {* Copyright (c) Julian M Bucknall 2000                  *}
  4. {* All rights reserved.                                  *}
  5. {*********************************************************}
  6. {* Algorithms Alfresco: Minimum heap replacement         *}
  7. {*********************************************************}
  8.  
  9. {Note: this unit is released as freeware. In other words, you are free
  10.        to use this unit in your own applications, however I retain all
  11.        copyright to the code. JMB}
  12.  
  13. unit AAHpMin;
  14.  
  15. {WARNING: this unit *must* appear first in your project's uses list.}
  16.  
  17. interface
  18.  
  19. implementation
  20.  
  21. uses
  22.   Windows; // it's OK to use the Windows unit: it allocates no memory
  23.  
  24. var
  25.   OrigHeap : TMemoryManager;
  26.   OurHeap  : TMemoryManager;
  27.  
  28. function OurGetMem(Size : integer) : pointer;
  29. begin
  30.   Result := OrigHeap.GetMem(Size);
  31. end;
  32.  
  33. function OurFreeMem(P : pointer) : integer;
  34. begin
  35.   Result := OrigHeap.FreeMem(P);
  36. end;
  37.  
  38. function OurReallocMem(P : pointer; Size : integer) : pointer;
  39. begin
  40.   Result := OrigHeap.ReallocMem(P, Size)
  41. end;
  42.  
  43. procedure InitializeUnit;
  44. begin
  45.   {get the original manager}
  46.   GetMemoryManager(OrigHeap);
  47.  
  48.   {set up our heap manager}
  49.   OurHeap.GetMem := OurGetMem;
  50.   OurHeap.FreeMem := OurFreeMem;
  51.   OurHeap.ReallocMem := OurReallocMem;
  52.  
  53.   {replace heap manager with ours}
  54.   SetMemoryManager(OurHeap);
  55. end;
  56.  
  57. procedure FinalizeUnit;
  58. begin
  59.   {restore the original manager}
  60.   SetMemoryManager(OrigHeap);
  61. end;
  62.  
  63.  
  64. initialization
  65.   InitializeUnit;
  66.  
  67. finalization
  68.   FinalizeUnit;
  69.  
  70. end.
  71.